package com.acme.exchange.gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.acme.exchange.exception.IllegalExchangeMessageData;
import com.acme.exchange.network.proto.MessageConstructor;
import com.acme.exchange.network.proto.Protocol.ExchangeMessage;
import com.acme.exchange.network.proto.Protocol.ExchangeMessage.OrderType;
import com.acme.exchange.network.proto.Protocol.ExchangeMessage.Side;

public class userDataEnter {

	private String userCommand;
	private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	private OrderType ot;
	private Side s;
	private String ticker;
	private int quantity;
	private String price;
	private int clOrderId;

	public userDataEnter() {
		clOrderId = 0;
	}

	 public ExchangeMessage loginEntering() throws IOException {
		 System.out.println("input a login > ");
		 userCommand = br.readLine(); // enter a userName			
		 ExchangeMessage em;
		 em = MessageConstructor.constructLoginMessage(userCommand);
		 System.out.println(em);
		 return em;
	 }

	public ExchangeMessage orderEntering() throws IOException {

		System.out.println("input a stock name > ");
		ticker = br.readLine();

		do {
			System.out.println("input an operation (sell(s)/buy(b)) > ");
			userCommand = br.readLine();
			if (!userCommand.equals("s") && !userCommand.equals("b")) {
				System.out.println("the wrong operation name");
				continue;
			} else {
				if (userCommand.equals("s")) {
					s = Side.SELL;
				} else {
					s = Side.BUY;
				}
				break;
			}
		} while (true);

		do {
			System.out.println("input a number of shares > ");
			userCommand = br.readLine();
			try {
				quantity = Integer.parseInt(userCommand);
			} catch (IllegalArgumentException e) {
				System.out.println("the wrong number of shares");
				continue;
			}
			break;
		} while (true);

		do {
			System.out.println("input an order type (limit(l)/market(m)) > ");
			userCommand = br.readLine();
			if (!userCommand.equals("l") && !userCommand.equals("m")) {
				System.out.println("the wrong order type");
				continue;
			} else {
				if (userCommand.equals("l")) {
					ot = OrderType.LIMIT;
				} else {
					ot = OrderType.MARKET;
				}
				break;
			}
		} while (true);

		if (ot.equals(OrderType.LIMIT)) {
			System.out.println("input a price > ");
			price = br.readLine();
		} else {
			price = "";
		}
		clOrderId++;
		ExchangeMessage em;
		em = MessageConstructor.constructNewOrder(clOrderId, ot, s, ticker, quantity, price);			
		System.out.println("to exit enter 'exit' ");
		System.out.println("to create a new order enter any button > ");
		userCommand = br.readLine();
		return em;
	}
	
	public String getUserCommand() {
		return userCommand;
	}
	
//	public static void main(final String args[]) {
//		userDataEnter ude = new userDataEnter();
//		ExchangeMessage newOrder;
//		do {
//			try {
//				newOrder = ude.orderEntering();
//				newOrder.writeDelimitedTo(System.out);
//			} catch (IOException | IllegalExchangeMessageData e) {
//				System.out.println("wrong input data");
//			}
//		}while (ude.getUserCommand().equals("con"));
//		
//	}

}
